package com.seven.dlandemo;

import android.util.Log;

import com.seven.dlandemo.entity.ClingDevice;
import com.seven.dlandemo.listener.DeviceListChangedListener;
import com.seven.dlandemo.service.manager.ClingDeviceManager;
import com.seven.dlandemo.service.manager.ClingManager;
import com.seven.dlandemo.ui.MainActivity;

import org.fourthline.cling.model.meta.Device;
import org.fourthline.cling.model.meta.LocalDevice;
import org.fourthline.cling.model.meta.RemoteDevice;
import org.fourthline.cling.registry.DefaultRegistryListener;
import org.fourthline.cling.registry.Registry;

public class QGameRegistryListener extends DefaultRegistryListener {

    private static final String TAG = MainActivity.myTAG + QGameRegistryListener.class.getSimpleName();
    private DeviceListChangedListener mOnDeviceListChangedListener;

    // todo delete
    private int tempCount = 0;

    public QGameRegistryListener() {
        super();
    }

    @Override
    public void remoteDeviceDiscoveryStarted(Registry registry, RemoteDevice device) {
        super.remoteDeviceDiscoveryStarted(registry, device);
        Log.d(TAG, "remoteDeviceDiscoveryStarted");
    }

    @Override
    public void remoteDeviceDiscoveryFailed(Registry registry, RemoteDevice device, Exception ex) {
        super.remoteDeviceDiscoveryFailed(registry, device, ex);
        Log.d(TAG, "remoteDeviceDiscoveryFailed");
        deviceRemoved(device);
    }

    @Override
    public void remoteDeviceAdded(Registry registry, RemoteDevice device) {
        super.remoteDeviceAdded(registry, device);
        Log.d(TAG, "remoteDeviceAdded");
        deviceAdded(device);
    }

    @Override
    public void remoteDeviceUpdated(Registry registry, RemoteDevice device) {
        super.remoteDeviceUpdated(registry, device);
        if (tempCount % 100 == 0) {
            // fixme delete 这个日志打印太多，手动减一减
            Log.d(TAG, "remoteDeviceUpdated");
        }
        tempCount ++;
    }

    @Override
    public void remoteDeviceRemoved(Registry registry, RemoteDevice device) {
        super.remoteDeviceRemoved(registry, device);
        Log.d(TAG, "remoteDeviceRemoved");
        deviceRemoved(device);
    }

    @Override
    public void localDeviceAdded(Registry registry, LocalDevice device) {
        super.localDeviceAdded(registry, device);
        Log.d(TAG, "localDeviceAdded");
    }

    @Override
    public void localDeviceRemoved(Registry registry, LocalDevice device) {
        super.localDeviceRemoved(registry, device);
        Log.d(TAG, "localDeviceRemoved");
    }

    @Override
    public void deviceAdded(Registry registry, Device device) {
        super.deviceAdded(registry, device);
        Log.d(TAG, "deviceAdded");
    }

    @Override
    public void deviceRemoved(Registry registry, Device device) {
        super.deviceRemoved(registry, device);
        Log.d(TAG, "deviceRemoved");
    }

    @Override
    public void beforeShutdown(Registry registry) {
        super.beforeShutdown(registry);
        Log.d(TAG, "beforeShutdown");
    }

    @Override
    public void afterShutdown() {
        super.afterShutdown();
        Log.d(TAG, "afterShutdown");
    }

    private void deviceAdded(Device device) {
        Log.d(TAG, "deviceAdded");
        if (!device.getType().equals(ClingManager.DMR_DEVICE_TYPE)) {
            Log.e(TAG, "deviceAdded called, but not match");
            return;
        }

        if (mOnDeviceListChangedListener != null) {
            ClingDevice clingDevice = new ClingDevice(device);
            ClingDeviceManager.getInstance().addDevice(clingDevice);
            mOnDeviceListChangedListener.onDeviceAdded(clingDevice);
        }
    }

    public void deviceRemoved(Device device) {
        Log.d(TAG, "deviceRemoved");
        if (mOnDeviceListChangedListener != null) {
            ClingDevice clingDevice = ClingDeviceManager.getInstance().getClingDevice(device);
            if (device != null) {
                ClingDeviceManager.getInstance().removeDevice(clingDevice);
                mOnDeviceListChangedListener.onDeviceRemoved(clingDevice);
            }
        }
    }

    public void setOnDeviceListChangedListener(DeviceListChangedListener onDeviceListChangedListener) {
        mOnDeviceListChangedListener = onDeviceListChangedListener;
    }
}
